import Promise from 'promise'
import ApiSend from './api-send'
import serConfig from '../api-config'
import ApiSendFile from './api-send-file'
import {name_to_dir} from '../filters/index'
let _ossNow = Date.parse(new Date()) / 1000;
let _ossData = {};

let serOss = {
    api:function (data) {
        _ossNow = Date.parse(new Date()) / 1000;
        if(_ossData._prefix!=data.dir||_ossData._time<(_ossNow-20)){
            return ApiSend(data,'OssPic/get2','get').then(function (res) {
                if (res.host.search('http://') == -1) {
                    res.host = "http://" + res.host
                }
                _ossData = res;
                _ossData._prefix = data.dir;
                _ossData._time = _ossNow;
                _ossData.paramsData = {
                    'key': serConfig.img_dir[data.dir] + '${filename}',
                    'policy': res['policy'],
                    'OSSAccessKeyId': res['accessid'],
                    'success_action_status': '200', //让服务端返回200,不然，默认会返回204
                    'callback': res['callback'],
                    'signature': res['signature'],
                };
                return {
                    host:_ossData.host,
                    data:_ossData.paramsData,
                }
            })
        }else {
            return new Promise(function (resolve) {
                resolve({
                    host:_ossData.host,
                    data:_ossData.paramsData,
                })
            })
        }
    },
    deletes:function (newList,oldList) {
        let obj = {},arr = [];
        newList.forEach((v)=>{
            obj[v.name] = true;
        });
        oldList.forEach((v)=>{
            if(!obj[v.name]){
                if(v.file_name){
                    arr.push(v.file_name);
                }
            }
        });
        obj = void 0;
        if(oldList.length>0){
            return ApiSend({
                picName:arr,
                dir:name_to_dir(oldList[0].file_name)
            },'OssPic/delosspic');
        }

    },
    upload:function (data) {
        return ApiSendFile(data)
    },
    name:function () {
        var d = new Date();
        var year = d.getFullYear()+"";
        var month = d.getMonth() + 1;
        var date = d.getDate();
        var day = d.getDay();
        var Hours=d.getHours(); //获取当前小时数(0-23)
        var Minutes=d.getMinutes(); //获取当前分钟数(0-59)
        var Seconds=d.getSeconds(); //获取当前秒数(0-59)
        var curDateTime = year;
        if (month > 9){curDateTime = curDateTime + month;
        }else{curDateTime = curDateTime + "0" + month;
        }if (date > 9){curDateTime = curDateTime + date;
        } else{curDateTime = curDateTime + "0" + date;
        }if (Hours > 9){curDateTime = curDateTime + Hours;
        } else{curDateTime = curDateTime + "0" + Hours;
        }if (Minutes > 9){curDateTime = curDateTime + Minutes;
        } else {curDateTime = curDateTime + "0" + Minutes;
        }if (Seconds > 9){curDateTime = curDateTime + Seconds;
        } else{ curDateTime = curDateTime + "0" + Seconds;}

        var rnd="";
        for(var i=0;i<6;i++){
            rnd+=Math.floor(Math.random()*10);
        }
        return curDateTime+rnd;
    },
};
export default serOss
